package com.msmall.community.user.action.html;

import java.io.File;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.util.ConstantAjax;
import com.msmall.base.util.Constants;
import com.msmall.community.user.model.UserInfo;
import com.msmall.community.user.model.UserProfile;
import com.msmall.community.user.service.UserManager;

public class UserAction extends HTMLBaseAction {

	private static final long serialVersionUID = -6868604712409430586L;

	private File avatar;
	private String avatarContentType;
	private String avatarFileName;
	
	private UserManager userManager;
	
	/**
	 * 进入上传AVATAR页面 
	 */
	public String uploadAvatar(){
		return SUCCESS;
	}
	/**
	 * 保存AVATAR
	 */
	public String saveAvatar(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			UserInfo user = userManager.saveAvatar(avatar, avatarFileName, getUserAgentType(), userid);
			if(user != null){
				buffer.append(createResult("true"))
				.append(ConstantAjax.CONTENT_PRE)
				.append(user.getAvatar())
				.append(ConstantAjax.CONTENT_SUFF);
			}else{
				throw new Exception();
			}
		}catch(Exception e){
			buffer.append(createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		return SUCCESS;
	}
	/**
	* 进入用户资料修改页面
	*/
	public String updateUser(){
		userInfo = userManager.getByUserid(getUserid());
		return SUCCESS;
	}
	/**
	 * 用户修改个人资料的修改
	 */
	public String saveUser(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			UserInfo oldu = userManager.getByUserid(userid);
			oldu.setNickname(userInfo.getNickname());
			oldu.setGender(userInfo.getGender());
			oldu.setEmail(userInfo.getEmail());
			oldu.setQq(userInfo.getQq());
			oldu.setMsn(userInfo.getMsn());
			oldu.setTel(userInfo.getTel());
			oldu.setBirthday(userInfo.getBirthday());
			String url = userInfo.getHomeUrl();
			if(StringUtils.isNotBlank(url)){
				if(!url.startsWith(Constants.HTTP)){
					url = Constants.HTTP + url;
				}
				oldu.setHomeUrl(url);
			}
			userManager.saveUserInfo(oldu);
			
			buffer.append(createResult("true"));
			
		}catch(Exception e){
			buffer.append(createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();
		
		return SUCCESS;
	}
	/**
	* 进入用户详细资料修改页面
	*/
	public String updateUserProfile(){
		userProfile = userManager.getProfileByUserid(getUserid());
		return SUCCESS;
	}
	
	/**
	* 保存用户详细资料修改
	*/
	public String saveUserProfile(){
		StringBuffer buffer = new StringBuffer();
		buffer.append(ConstantAjax.RETURN_PRE);
		try{
			UserProfile oldp = userManager.getProfileByUserid(userid);
			oldp.setAddress(userProfile.getAddress());
			oldp.setAsterism(userProfile.getAsterism());
			oldp.setBloodType(userProfile.getBloodType());
			oldp.setBodyType(userProfile.getBodyType());
			oldp.setCharacterise(userProfile.getCharacterise());
			oldp.setDrink(userProfile.getDrink());
			oldp.setEduDegree(userProfile.getEduDegree());
			oldp.setEduEnrolYear(userProfile.getEduEnrolYear());
			oldp.setEduSchool(userProfile.getEduSchool());
			oldp.setEduSpecialty(userProfile.getEduSpecialty());
			oldp.setFavBook(userProfile.getFavBook());
			oldp.setFavIdol(userProfile.getFavIdol());
			oldp.setFavMoive(userProfile.getFavMoive());
			oldp.setFavOthers(userProfile.getFavOthers());
			oldp.setFavSports(userProfile.getFavSports());
			oldp.setHeight(userProfile.getHeight());
			oldp.setHometown(userProfile.getHometown());
			oldp.setWeight(userProfile.getWeight());
			oldp.setSmoke(userProfile.getSmoke());
			oldp.setSalary(userProfile.getSalary());
			oldp.setResidence(userProfile.getResidence());
			oldp.setPostal(userProfile.getPostal());
			oldp.setMarried(userProfile.getMarried());
			oldp.setJobCompany(userProfile.getJobCompany());
			oldp.setJobEnrolYear(userProfile.getJobEnrolYear());
			oldp.setJobTitle(userProfile.getJobTitle());
			oldp.setJobProfession(userProfile.getJobProfession());
			oldp.setModifyTime(new Date());
			userManager.saveObject(oldp);
			buffer.append(createResult("true"));
		}catch(Exception e){
			buffer.append(createResult("false"));
		}
		buffer.append(ConstantAjax.RETURN_SUFF);
		message = buffer.toString();	
		return SUCCESS;
	}

	public String viewStatistics(){
		userInfo = userManager.getByUserid(getUserid());
		userProfile = userManager.getProfileByUserid(getUserid());
		return SUCCESS;
	}
	public String viewInfo(){
		userInfo = userManager.getByUserid(getUserid());
		return SUCCESS;
	}
	public String viewProfile(){
		userProfile = userManager.getProfileByUserid(getUserid());
		return SUCCESS;
	}
	
	public File getAvatar() {
		return avatar;
	}
	public void setAvatar(File avatar) {
		this.avatar = avatar;
	}
	public String getAvatarContentType() {
		return avatarContentType;
	}
	public void setAvatarContentType(String avatarContentType) {
		this.avatarContentType = avatarContentType;
	}
	public String getAvatarFileName() {
		return avatarFileName;
	}
	public void setAvatarFileName(String avatarFileName) {
		this.avatarFileName = avatarFileName;
	}
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
}
